//
//  CMSecondLevelViewController.m
//  BlackBambooCM-OC
//
//  Created by 宜凯门-DKang on 2018/10/11.
//  Copyright © 2018年 ucanmax. All rights reserved.
//

#import "CMSecondLevelViewController.h"
#import "SecondLevelCell.h"
//新增意向单
#import "NewAddIntentionSheetViewController.h"

static NSString *secondLevelId = @"SecondLevelCell";

@interface CMSecondLevelViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *secondTableView;
@property (strong, nonatomic) NSMutableArray *homeItemsTitleArray;
@property (retain, nonatomic) NSString *gridId;
@property (retain, nonatomic) NSArray *homeArray;
@property (retain, nonatomic) NSMutableArray *childrenArray;
@end

@implementation CMSecondLevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.gridItem.gridTitle;
    [self.secondTableView registerNib:[UINib nibWithNibName:@"SecondLevelCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:secondLevelId];
    [self addNavigationItemWithImgs:@[@"backWhite"] isLeft:YES  target:self action:@selector(backBtnClicked) tags:@[@(0)]];
    [self initData];
}

- (void) initData {
    self.gridId = [NSString stringWithFormat:@"%ld",(long)self.gridItem.gridId];
    self.homeArray = [CMHelper readLocalFileWithName:@"functions"];
    self.homeItemsTitleArray = [NSMutableArray new];
    self.childrenArray = [NSMutableArray new];
    for (NSDictionary*dict in self.homeArray) {
        NSDictionary *secDict = dict[self.gridId];
        if(secDict){
            if(secDict[@"children"]){
                for (NSDictionary *d in secDict[@"children"]) {
                    [self.childrenArray addObject:d];
                }
            }
        }
        
        
    }
    for (NSDictionary *childrenDict in self.childrenArray) {
        [_homeItemsTitleArray addObject:childrenDict[@"name"]];
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SecondLevelCell *cell = [tableView dequeueReusableCellWithIdentifier:secondLevelId];
    if (!cell){
        cell = [[SecondLevelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:secondLevelId];
    }
    NSString *title = self.homeItemsTitleArray[indexPath.row];
    [cell updateCell:title];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.homeItemsTitleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *childrenDict = self.childrenArray[indexPath.row];
    NSInteger childrenId = [childrenDict[@"functionId"] integerValue];
    [self didSelectRow:childrenId];
}

- (void)didSelectRow:(NSInteger)childrenId {
    switch (childrenId) {
        //基本信息管理
        case 101:{
            
        }
            break;
        case 102:{
            
        }
            break;
        case 103:{
            
        }
            break;
        case 104:{
            
        }
            break;
        case 105:{
            
        }
            break;
        case 106:{
            
        }
            break;
        case 107:{
            
        }
            break;
        case 108:{
            
        }
            break;
        case 109:{
            
        }
            break;
        case 110:{
            
        }
            break;
        case 111:{
            
        }
            break;
        case 112:{
            
        }
            break;
        case 113:{
            
        }
            break;
        //采购管理
        case 201:{
            //新增意向单
            NewAddIntentionSheetViewController *newAddVC = [[NewAddIntentionSheetViewController alloc] initWithNibName:@"NewAddIntentionSheetViewController" bundle:[NSBundle mainBundle]];
            [self.navigationController pushViewController:newAddVC animated:YES];
            
        }
            break;
        case 202:{
            
        }
            break;
        case 203:{
            
        }
            break;
        case 204:{
            
        }
            break;
        case 205:{
            
        }
            break;
        case 206:{
            
        }
            break;
            
        default:
            break;
    }
}

@end
